package com.zmc.product.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.zmc.product.interceptor.MyBatisPlusShardingInterceptor;
import com.zmc.product.interceptor.MyBatisShardingInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.spring.boot.rule.YamlShardingRuleSpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * @Author：zmc
 */
@Slf4j
@AutoConfigureAfter(MybatisPlusConfig.class)
public class ShardingInterceptorConfig {
    @Resource
    private MybatisPlusInterceptor mybatisPlusInterceptor;
    @Resource
    private YamlShardingRuleSpringBootConfiguration yamlShardingRuleSpringBootConfiguration;

    @Bean
    public MyBatisShardingInterceptor myBatisShardingInterceptor() {
        log.info("MyBatisShardingInterceptor init");
        // 自定义拦截器- 兼容updateById,无法分表问题
        mybatisPlusInterceptor.addInnerInterceptor(new MyBatisPlusShardingInterceptor(yamlShardingRuleSpringBootConfiguration.getSharding().getTables()));
        return new MyBatisShardingInterceptor(yamlShardingRuleSpringBootConfiguration.getSharding().getTables());
    }

}
